package com.shark.uh.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.shark.uh.enums.PermissionStatus;
import com.shark.uh.model.dto.permission.PermissionCreateDTO;
import com.shark.uh.model.dto.permission.PermissionResponseDTO;
import com.shark.uh.model.dto.roles.RolesResponseDTO;
import com.shark.uh.model.query.PermissionQuery;
import com.shark.uh.model.vo.PermissionVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shark.uh.model.entity.Permissions;
import com.shark.uh.service.PermissionsService;
import com.shark.uh.mapper.PermissionsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class PermissionsServiceImpl extends ServiceImpl<PermissionsMapper, Permissions>
    implements PermissionsService{

    @Resource
    private PermissionsMapper permissionsMapper;

    /**
     * @param permissionCreateDTO
     */
    @Override
    public void savePermissions(PermissionCreateDTO permissionCreateDTO) {
        Permissions permissions = new Permissions();
        BeanUtils.copyProperties(permissionCreateDTO, permissions);

        //时间
        Date now = new Date();
        permissions.setCreatedAt(now);
        permissions.setUpdatedAt(now);

        //状态
        permissions.setStatus(PermissionStatus.ACTIVE.getStatus());

        // 创建者
        permissions.setCreatedBy(StpUtil.getLoginIdAsLong());

        save(permissions);
    }

    /**
     * @param permissionIds
     */
    @Override
    public void deletePermissions(List<Long> permissionIds) {
        // todo 权限校验
        permissionsMapper.deleteBatchIds(permissionIds);
    }

    /**
     * @param permissionQuery
     * @return
     */
    @Override
    public PermissionResponseDTO findPermissions(PermissionQuery permissionQuery) {
        permissionQuery.setOffset((permissionQuery.getPageNo() - 1) * permissionQuery.getPageSize());
        List<Permissions> permissions = permissionsMapper.selectConditional(null,null,0,10);
        PermissionResponseDTO permissionResponseDTO = new PermissionResponseDTO();
        List<PermissionVO> permissionVOS = new ArrayList<>();
        PermissionVO permissionVO = new PermissionVO();
        for (Permissions permission : permissions) {
            BeanUtils.copyProperties(permission, permissionVO);
            permissionVOS.add(permissionVO);
        }
        permissionResponseDTO.setPermissionVOS(permissionVOS);
        return permissionResponseDTO;
    }

    /**
     * @param permissionId
     * @param roleIds
     */
    @Override
    public void assignRoles(Long permissionId, List<Long> roleIds) {
        permissionsMapper.insertRelationBatch(permissionId, roleIds);
    }

    /**
     * @param permissionId
     * @param roleIds
     */
    @Override
    public void removeRoles(Long permissionId, List<Long> roleIds) {
        permissionsMapper.deleteRelationBatch(permissionId, roleIds);
    }

    /**
     * @param permissionId
     * @return
     */
    @Override
    public RolesResponseDTO getAssignRoles(Long permissionId) {
//        List<Roles> roles = permissionsMapper.getRolesByPermissionId(permissionId);
//        RoleVO roleVO = new RoleVO();
//        List<RoleVO> roleVOS = new ArrayList<>();
//        for (Roles role : roles) {
//            BeanUtils.copyProperties(role, roleVO);
//            roleVOS.add(roleVO);
//        }
//        RolesResponseDTO rolesResponseDTO = new RolesResponseDTO();
//        rolesResponseDTO.setRoles(roleVOS);
//        return rolesResponseDTO;
        return null;
    }


}




